<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function myaxios({method,url,params,data}) {
            let arr = []
            for (const key in params) {
              arr.push(`${key}=${params[key]}`)
            }
            // console.log(arr.join('&'));
            const xhr = new XMLHttpRequest()
           xhr.addEventListener('load',()=>{
            if (xhr.status >= 200 && xhr.status < 300) {
          console.log('成功', JSON.parse(xhr.response))
        } else if (xhr.status >= 300) {
          console.log('失败', JSON.parse(xhr.response))
        }
         })
        xhr.open(method,url+'?'+arr.join('&'))
        if (data instanceof FormData) {
            xhr.send(data)
        }else if (data instanceof Object) {
            xhr.setRequestHeader('Content-Type', 'application/json')
            xhr.send(JSON.stringify(data))
        }else if (data instanceof String) {
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
            xhr.send(data)
        }else{
            xhr.send()
        }
        }
      

      myaxios({
      method: 'GET',
      url: 'http://www.itcbc.com:3006/api/getbooks',
      params: {
        appkey: '987411',
        bookname:'111'
      }
    })
    myaxios({
      method: 'POST',
      url: 'http://www.itcbc.com:3006/api/addbook',
      data: {
        bookname: '西游记',
        author: '吴承恩',
        publisher: '北京出版社',
        appkey: 'laotang1234'
      }
    })

    </script>
</body>
</html>